// pages/Search/Search.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        hotSearch: ['牛奶', '鸡蛋', '盐', '食用油', '饮料', '零食'],
        hisSearch: []
    },
    handleDelete() {
        var hisSearch = this.data.hisSearch;
        hisSearch = [];
        wx.setStorageSync('hisSearch', hisSearch)
        this.setData({
            hisSearch
        })
    },
    onSearch(e) {
        var hisSearch = this.data.hisSearch;
        if (!hisSearch.includes(e.detail)) {
            hisSearch.unshift(e.detail);
        } else {
            var index = hisSearch.indexOf(e.detail);
            hisSearch.splice(index, 1);
            hisSearch.unshift(e.detail)
        }
        wx.setStorageSync('hisSearch', hisSearch)
        this.setData({
            hisSearch
        })
        wx.navigateTo({
            url: `/pages/Search/searchResult/searchResult?keyword=${e.detail}`,
        })
    },
    handleSearch(e) {
        var { item } = e.currentTarget.dataset;
        var hisSearch = this.data.hisSearch
        if (!hisSearch.includes(item)) {
            hisSearch.unshift(item);
        } else {
            var index = hisSearch.indexOf(item);
            hisSearch.splice(index, 1);
            hisSearch.unshift(item)
        }
        wx.setStorageSync('hisSearch', hisSearch)
        this.setData({
            hisSearch
        })
        wx.navigateTo({
            url: `/pages/Search/searchResult/searchResult?keyword=${item}`,
        })
    },
    onCancel() {
        wx.navigateBack({
          delta: 1,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var hisSearch = wx.getStorageSync('hisSearch')
        if (hisSearch) {
            this.setData({
                hisSearch
            })
        }

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})